home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 5 / BBS in a Box -Volume V (BBS in a Box) (April 1992).iso / Files / Prog / M / Mac gperf 1.9.cpt / Mac gperf 1.9 / src / iterator.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-09  |  3.4 KB  |  113 lines  |  [TEXT/KAHL]

  1. /* Provides an Iterator for keyword characters.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  4.  
  5. This file is part of GNU GPERF.
  6.  
  7. GNU GPERF is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU GPERF is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU GPERF; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. #include "iterator.h"
  25.  
  26. /* Locally visible ITERATOR object. */
  27.  
  28. ITERATOR iterator;
  29.  
  30.     /***********************************************************************\
  31.     *                                                                        *
  32.     * name:        iterator_init                                                *
  33.     *                                                                        *
  34.     * descr:    Constructor for ITERATOR.                                    *
  35.     *                                                                        *
  36.     \***********************************************************************/
  37.  
  38. void iterator_init( char *s, int lo, int hi, int word_end, int bad_val, int key_end )
  39. {
  40.   iterator.end         = key_end;
  41.   iterator.error_value = bad_val;
  42.   iterator.end_word    = word_end;
  43.   iterator.str         = s;
  44.   iterator.hi_bound    = hi;
  45.   iterator.lo_bound    = lo;
  46. }
  47.  
  48. /* Define several useful macros to clarify subsequent code. */
  49. #define ISPOSDIGIT(X) ((X)<='9'&&(X)>'0')
  50. #define TODIGIT(X) ((X)-'0')
  51.  
  52.  
  53.     /***********************************************************************\
  54.     *                                                                        *
  55.     * name:        next                                                        *
  56.     *                                                                        *
  57.     * descr:    Provide an Iterator, returning the "next" value from the    *
  58.     *            list of valid values given in the constructor.                *
  59.     *                                                                        *
  60.     \***********************************************************************/
  61.  
  62. int next()
  63. /* Variables to record the Iterator's status when handling ranges, e.g., 3-12. */
  64.  
  65.   static int size;              
  66.   static int curr_value;           
  67.   static int upper_bound;
  68.  
  69.   if (size) 
  70.     { 
  71.       if (++curr_value >= upper_bound) 
  72.         size = 0;    
  73.       return curr_value; 
  74.     }
  75.   else 
  76.     {
  77.       while (*iterator.str) 
  78.         {
  79.           if (*iterator.str == ',') 
  80.             iterator.str++;
  81.           else if (*iterator.str == '$') 
  82.             {
  83.               iterator.str++;
  84.               return iterator.end_word;
  85.             }
  86.           else if (ISPOSDIGIT (*iterator.str))
  87.             {
  88.  
  89.               for (curr_value = 0; isdigit (*iterator.str); iterator.str++) 
  90.                 curr_value = curr_value * 10 + *iterator.str - '0';
  91.  
  92.               if (*iterator.str == '-') 
  93.                 {
  94.  
  95.                   for (size = 1, upper_bound = 0; 
  96.                        isdigit (*++iterator.str); 
  97.                        upper_bound = upper_bound * 10 + *iterator.str - '0');
  98.  
  99.                   if (upper_bound <= curr_value || upper_bound > iterator.hi_bound) 
  100.                     return iterator.error_value;
  101.                 }
  102.               return curr_value >= iterator.lo_bound && curr_value <= iterator.hi_bound 
  103.                 ? curr_value : iterator.error_value;
  104.             }
  105.           else
  106.             return iterator.error_value;               
  107.         }
  108.  
  109.       return iterator.end;
  110.     }
  111. }
  112.